package cn.goktech.controller;

import cn.goktech.bean.User;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.servlet.ModelAndView;

@Controller
public class ModelContronller {
    /*
    * model作为方法的参数可以在request作用域中存放数据
    * */
    @RequestMapping("/respModel1")
    public String respModelControl(Model model){
        model.addAttribute("str","string");
        model.addAttribute("user",new User());
        model.addAttribute("id",100);
        return "model";
    }

    /**
     *
     * ModelAndView：
     * 包含存放数据的作用域以及转发的视图
     * modelAndView.setViewName（“视图名称”）；设置方法返回的视图
     * modelAndView.addObject（key,value）；设置作用域中是数据参数及value
     *
     */
    @RequestMapping("/respModel2")
    public ModelAndView respModelControl(ModelAndView modelAndView){
        modelAndView = new ModelAndView();
        modelAndView.setViewName("model2");
        modelAndView.addObject("str","goktech");
        modelAndView.addObject("user",new User());
        modelAndView.addObject("id",100);
        return modelAndView;
    }
}
